package com.xiangyueEducation.uploaderCloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyueEducation.uploaderCloud.context.BaseContext;
import com.xiangyueEducation.uploaderCloud.mapper.DiagnoseMapper;
import com.xiangyueEducation.uploaderCloud.mapper.DoctorMapper;
import com.xiangyueEducation.uploaderCloud.pojo.dto.PageQuery;
import com.xiangyueEducation.uploaderCloud.pojo.dto.StudenteFeedbackDTO;
import com.xiangyueEducation.uploaderCloud.pojo.entity.Diagnose;
import com.xiangyueEducation.uploaderCloud.pojo.entity.Doctor;
import com.xiangyueEducation.uploaderCloud.pojo.entity.User;
import com.xiangyueEducation.uploaderCloud.pojo.vo.PageVo;
import com.xiangyueEducation.uploaderCloud.service.DiagnoseService;
import com.xiangyueEducation.uploaderCloud.utils.enums.AppointmentStatus;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DiagnoseServiceImpl extends ServiceImpl<DiagnoseMapper, Diagnose> implements DiagnoseService {
    @Resource
    private DiagnoseMapper diagnoseMapper;
    @Resource
    private DoctorMapper doctorMapper;
    @Override
    public void appointment(Diagnose diagnose) {
        //设置状态为未读
        diagnose.setStatus(AppointmentStatus.UNREAD.getCode());
        diagnoseMapper.insert(diagnose);
    }

    @Override
    public Long count(Long id) {
        //查询条件
        QueryWrapper<Diagnose> diagnoseQueryWrapper = new QueryWrapper<>();
        //查询状态是未查看且是医生的诊断信息
        diagnoseQueryWrapper.eq("doctor_user_id",id);
        diagnoseQueryWrapper.eq("status",AppointmentStatus.UNREAD.getCode());
        return diagnoseMapper.selectCount(diagnoseQueryWrapper);
    }

    @Override
    public PageVo getPages(PageQuery pageQuery) {
        User currentUser = BaseContext.getCurrentUser();
        Integer userRoleId = currentUser.getRoleId();
        Long id = currentUser.getId();
        QueryWrapper<Diagnose> diagnoseQueryWrapper = new QueryWrapper<>();
        if (userRoleId.equals(0)){
            diagnoseQueryWrapper.eq("doctor_user_id", id);
        }else {
            diagnoseQueryWrapper.eq("patient_user_id",id);
        }
        Integer pageSize = pageQuery.getPageSize();
        Integer pageNo = pageQuery.getPageNo();
        //添加分页基本参数
        Page<Diagnose> doctorPage = new Page<>(pageNo, pageSize);
        //去数据库查
        Page<Diagnose> page = page(doctorPage,diagnoseQueryWrapper);
        List<Diagnose> records = page.getRecords();
        long total = page.getTotal();
        return new PageVo(total,records);
    }

    @Override
    public void updateAllById(Diagnose diagnose) {
        diagnoseMapper.updateAll(diagnose);
    }

    @Override
    public void updateStatusById(Diagnose diagnose) {
        diagnoseMapper.updateById(diagnose);
    }
    @Transactional
    @Override
    public void mark(StudenteFeedbackDTO studenteFeedbackDTO) {
        //查询诊断信息
        Diagnose diagnose = diagnoseMapper.selectById(studenteFeedbackDTO.getId());
        //利用诊断信息出是哪位医生
        Long doctorUserId = diagnose.getDoctorUserId();
        if (doctorUserId==null)return;
        //查询医生原本多少分
        Map<String,Object> map = new HashMap<>();
        map.put("user_id",doctorUserId);
        List<Doctor> doctors = doctorMapper.selectByMap(map);
        Doctor doctor = doctors.get(0);
        Integer rating = doctor.getRating();
        //第一次给评分
        if(rating==null){
            doctor.setRating(studenteFeedbackDTO.getRating());
            doctorMapper.updateById(doctor);
            return;
        }
        //多少个人给过评价
        //查询条件
        QueryWrapper<Diagnose> diagnoseQueryWrapper = new QueryWrapper<>();
        //查询状态是未查看且是医生的诊断信息
        diagnoseQueryWrapper.eq("doctor_user_id",doctorUserId);
        Long count = diagnoseMapper.selectCount(diagnoseQueryWrapper);
        //计算新评分，并将long转化为int
        Integer newRating = Math.toIntExact( (rating * count + studenteFeedbackDTO.getRating()) / (count + 1));
        doctor.setRating(newRating);
        doctorMapper.updateById(doctor);
    }
}
